<script setup>
import { ref } from 'vue'
import { emailApi } from '../../../services'



const reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
const email = ref('')
const password = ref('')


const loginEmail = async() => {
    try {
        const res = await emailApi(email.value,password.value)
        console.log(res)
        if(res.code === 200) {
            uni.showToast({
                title:'登陆成功',
                icon:'success'
            })
            uni.switchTab({
                url:'/pages/index/index'
            })
            uni.setStorageSync('curCookie',res.cookie)
        }else{
            uni.showToast({
                title:res.message,
                icon:'error'
            })
        }
    }catch(e) {
        uni.showToast({
            title:'登录失败',
            icon:'error'
            
        })
        console.log(e)
    }
}

const submit = () => {
    if(email.value.length === 0 && password.value.length === 0) {
        uni.showToast({
            title:'请输入邮件和密码',
            icon:'error'
        })
    }else{
        if(reg.test(email.value)) {
            loginEmail()
        }else{
            uni.showToast({
            title:'邮箱格式错误,请输入正确的邮箱格式',
            icon:'error'
            })
        }
    }
}

</script>

<template>
    <view class="email-login">
        <view class="row">
            <input type="email" v-model="email" placeholder="请输入邮箱">
        </view>
        <view class="row">
            <input type="password" v-model="password" placeholder="请输入密码">
        </view>
		<button @click="submit" type="primary">登录</button>
    </view>
</template>

<style lang="scss" type="primary" scoped>
.row{
    margin-bottom: 10px;
    input{
        height: 40px;
        width: 100%;
        border-radius: 10px;
        border: 1px solid #eee;
        padding: 0 20px;
    }
}
</style>